package com.ai.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.common.analyze.CipherUtils;
import com.ai.common.controller.BaseController;
import com.ai.common.model.Result;
import com.ai.mmd.reviewed.dto.DlxxDTO;
import com.ai.mmd.reviewed.dto.QyshclDTO;
import com.ai.mmd.reviewed.dto.YhxxDTO;
import com.ai.service.reviewed.model.DlxxModel;
import com.ai.service.reviewed.model.QyshclModel;
import com.ai.service.reviewed.model.YhxxModel;
import com.ai.service.reviewed.service.IDlxxService;
import com.ai.service.reviewed.service.IQyshclService;
import com.ai.service.reviewed.service.IYhxxService;

/**
 * 
 * Class Name : RegisterController<br>
 * 
 * Description : 注册Controller类<br>
 * 
 * @see
 *
 */
@RequestMapping("login")
@Controller("com.ai.controller.RegisterController")
public class RegisterController extends BaseController {

	@Resource(name = "iyhxxService")
	private IYhxxService iyhxxService;
	@Resource(name = "idlxxService")
	private IDlxxService idlxxService;
	@Resource(name = "iqyshclService")
	private IQyshclService iqyshclService;
	@Value("${mainDomain}")
	private String mainDomain;

	/**
	 * Description ：注册引导
	 * @throws Exception 
	 */
	@RequestMapping("/registerindex")
	public String registerindex() throws Exception {
		
		return "/front/frontgoods/register";
	}
	
	/**
	 * Description ：登录引导
	 * @throws Exception 
	 */
	@RequestMapping("/loginindex")
	public String loginindex(@RequestParam(required = false) String tourl,
			@RequestParam(required = false) String url) throws Exception {
		
		Result result = new Result(true);
		if (tourl!=null) {
			result.addAttribute("tourl", tourl);
		}else {
//			String url = request.getHeader("Referer");
//			if (url!=null&&url.indexOf("61.183.190.26")>0) {
//				result.addAttribute("tourl", url);
//			}
			if (url!=null) {
				result.addAttribute("hdurl", url);
			}
		}
		result.rendering();
		
		return "/front/frontgoods/login";
	}
	
	/**
	 * Description ：注册提交
	 * @throws Exception 
	 */
	@RequestMapping(value ="/registerconfig", method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> registerconfig(DlxxDTO dlxx) throws Exception {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		DlxxModel dlxxModel = idlxxService.getOne(new DlxxDTO(dlxx.getYhid()));
		if (dlxxModel!=null) {
			map.put("message", "用户名已存在");
			return map;
		}
		
		dlxxModel = idlxxService.getOne(dlxx);
		if (dlxxModel!=null) {
			map.put("message", "手机号已存在");
			return map;
		}
		
		dlxxModel = new DlxxModel();
		BeanUtils.copyProperties(dlxx, dlxxModel);
		String MD5pwd = CipherUtils.MD5Encode(dlxxModel.getMm());
		dlxxModel.setMm(MD5pwd);
		String uuid = UUID.randomUUID().toString()+ new Date().toString();
		String token = CipherUtils.MD5Encode(uuid);
				
		dlxxModel.setToken(token);
		dlxxModel.setScdlsj(new Date());
		dlxxModel.setCjsj(new Date());
		dlxxModel.setGxsj(new Date());
		idlxxService.insert(dlxxModel);
		
		map.put("message", 1);
		
		return map;
	}
	
		
	/**
	 * Description ：登录操作
	 * @throws Exception 
	 */
	@RequestMapping("/login")
	public @ResponseBody Map<String,Object> login(DlxxDTO dlxx) throws Exception {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		String MD5pwd = CipherUtils.MD5Encode(dlxx.getMm());
		dlxx.setMm(MD5pwd);
		
		DlxxModel dlxxParam = idlxxService.getOne(dlxx);
		if (dlxxParam!=null) {
			//判断注册步骤
			//1.如果yhxx表没数据，则进入注册第二步
			YhxxModel yhxx = iyhxxService.getOne(new YhxxDTO(dlxx.getYhid()));
			if (yhxx==null) {
				map.put("message", -1);
				map.put("tostep", "registertwo");
				return map;
			}
			
			//2.如果qyshcl没数据,则进入注册第三步。个人用户不判断注册第三步
			if (yhxx.getYhlx()!=8) {
				List<QyshclModel> list = iqyshclService.getList(new QyshclDTO(dlxx.getYhid()));
				if (list.size()==0) {
					map.put("message", -1);
					map.put("tostep", "registerthree");
					return map;
				}
			}
			
			//判断是否冻结
			if (dlxxParam.getSfdj()==1) {
				//已冻结
				map.put("message", "该用户已冻结");
				return map;
			}
			
			//判断是否审核通过
			if (yhxx.getYhzt()!=1) {
				//审核未通过
				map.put("message", -2);
				return map;
				
			}else {
				//审核通过
				//生成随机key
				String uuid = UUID.randomUUID().toString()+ new Date().toString();
				String token = CipherUtils.MD5Encode(uuid);
				
				dlxxParam.setToken(token);
				dlxxParam.setScdlsj(new Date());
				dlxxParam.setGxsj(new Date());
				idlxxService.updateByPrimaryKey(dlxxParam);
				
				map.put("message", 1);
				map.put("mapyhid", dlxxParam.getYhid());
				map.put("maptoken", dlxxParam.getToken());
				map.put("url", request.getSession().getAttribute("url"));
				map.put("csid", request.getSession().getAttribute("csid"));
				session.setAttribute("yhid", dlxxParam.getYhid());
				session.setAttribute("token", dlxxParam.getToken());			
				session.setAttribute("yhxxModel", yhxx);
			}		
		}else {
			map.put("message", "登录失败，请核对您的用户名和密码");
		}
		
		return map;
	}
	
	/**
	 * Description ：登出操作
	 * @throws Exception 
	 */
	@RequestMapping("/loginout")
	public String  loginout(DlxxDTO dlxx) throws Exception {
		
		session.removeAttribute("yhid");
		session.removeAttribute("token");
		session.removeAttribute("yhxxModel");
		session.removeAttribute("url");
		
		return "redirect:/index";
	}
}
